/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.catosist.run.business.gestorComprobanteDetalle;

import com.catosist.run.dataaccess.domain.ComprobanteDetalleDao;
import com.catosist.run.dataaccess.model.ComprobanteVO;
import com.catosist.run.dataaccess.model.ComprobantedetalleVO;
import com.catosist.run.dataaccess.model.GuiaVO;
import com.catosist.run.services.ComprobanteDetalleDTO;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author igorov
 */
public class GestorComprobanteDetalleImpl implements GestorComprobanteDetalle{
    private ComprobanteDetalleDao comprobanteDetalleDao;

    public ComprobanteDetalleDao getComprobanteDetalleDao() {
        return comprobanteDetalleDao;
    }

    public void setComprobanteDetalleDao(ComprobanteDetalleDao comprobanteDetalleDao) {
        this.comprobanteDetalleDao = comprobanteDetalleDao;
    }

    public List<ComprobanteDetalleDTO> getByComprobanteId(int comprobanteId) {
        List<ComprobantedetalleVO> comprobantedetalleVOs = comprobanteDetalleDao.getByComprobanteId(comprobanteId);
        List<ComprobanteDetalleDTO> comprobanteDetalleDTOs = new LinkedList<ComprobanteDetalleDTO>();
        for (Iterator<ComprobantedetalleVO> it = comprobantedetalleVOs.iterator(); it.hasNext();) {
            ComprobantedetalleVO comprobantedetalleVO = it.next();
            ComprobanteDetalleDTO comprobanteDetalleDTO = new ComprobanteDetalleDTO(comprobantedetalleVO.getId(),
                    comprobantedetalleVO.getProducto(),
                    comprobantedetalleVO.getCantidad(),
                    comprobantedetalleVO.getPreciounitario(),
                    comprobantedetalleVO.getTotal());
            if(comprobantedetalleVO.getGuia() != null)
                comprobanteDetalleDTO.setGuiaId(comprobantedetalleVO.getGuia().getId());
            else
                comprobanteDetalleDTO.setGuiaId(0);

            comprobanteDetalleDTO.setComprobanteId(comprobantedetalleVO.getComprobante().getId());
            
            comprobanteDetalleDTOs.add(comprobanteDetalleDTO);
        }
        return comprobanteDetalleDTOs;
    }

    public void saveOrUpdate(ComprobanteDetalleDTO comprobanteDetalleDTO, int comprobanteId) {
        System.out.println("Creando guia en mem con id " + comprobanteId);
        ComprobanteVO comprobanteVO = new ComprobanteVO();
        comprobanteVO.setId(comprobanteId);

        

        ComprobantedetalleVO comprobantedetalleVO = new ComprobantedetalleVO();
        if(comprobanteDetalleDTO.getId() != 0)
            comprobantedetalleVO.setId(comprobanteDetalleDTO.getId());
        comprobantedetalleVO.setCantidad(comprobanteDetalleDTO.getCantidad());
        comprobantedetalleVO.setComprobante(comprobanteVO);
        comprobantedetalleVO.setPreciounitario(comprobanteDetalleDTO.getPreciounitario());
        comprobantedetalleVO.setProducto(comprobanteDetalleDTO.getProducto());
        comprobantedetalleVO.setTotal(comprobanteDetalleDTO.getTotal());
        
        if (comprobanteDetalleDTO.getGuiaId() != 0) {
            GuiaVO guiaVO = new GuiaVO();
            System.out.println("Guardando de comprobante detalle, guia "+comprobanteDetalleDTO.getGuiaId());
            guiaVO.setId(comprobanteDetalleDTO.getGuiaId());
            comprobantedetalleVO.setGuia(guiaVO);
        }


        System.out.println("guardando objeto: id comprobante: " + comprobanteId);
        System.out.println("DetalleId: " + comprobanteDetalleDTO.getId());
        System.out.println("Cantidad: " + comprobanteDetalleDTO.getCantidad());
        System.out.println("Precio unitario: " + comprobanteDetalleDTO.getPreciounitario());
        System.out.println("Producto: " + comprobanteDetalleDTO.getProducto());
        System.out.println("Total: " + comprobanteDetalleDTO.getTotal());


        comprobanteDetalleDao.saveOrUpdate(comprobantedetalleVO);
    }



    public void deleteById(int i) {
        comprobanteDetalleDao.deleteById(i);
    }

    public void deleteByIndex(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List getAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object getById(int voId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object getByIndex(int indice) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getIdAtIndex(int indice) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getIndexOf(int voId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void saveOrUpdate(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void updateListVo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


}
